<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cate;

class CateController extends Controller
{

    // 删除分类
    public function delete(Request $request){
        $cate = Cate::find($request->id);
        $cate->delete();
        return response()->json(['status'=>200,'msg'=>'删除成功!','data'=>$cate]);
    }

    // 保存分类
    public function store(Request $request){

        // 上传分类图片
        $file = $request->file("pic");
        $cateArr = json_decode($request->form,true);

        $picName = "";

        if($file){
            $path = \base_path("/public/upload/");
            $ext = $file->getClientOriginalExtension();
            $picName = time().rand(0,999).".".$ext;
            $file->move($path,$picName);
           
        }

        $cateArr['img'] = $picName;
        unset($cateArr['pic']);


        // 保存分类
        Cate::create($cateArr);
        return response()->json(['status'=>200,'msg'=>'保存成功!','data'=>$cateArr]);

    }

    

    // 分类列表
    public function list(){

       $cates = Cate::where('pid',0)
       ->orderBy('ord','desc')->get();
       foreach($cates as $k=>$cate){
          $cates[$k]['children'] = $this->getChildren($cate->id);
       }

       return $cates;

       
    }

    // 获取子类
    private function getChildren($pid){

        $cates = Cate::where('pid',$pid)
        ->orderBy('ord','desc')->get();

        foreach($cates as $k=>$cate){
            if(strlen($cate->img)>6){
                $cates[$k]['img'] = config('app.upload').$cate->img;
            }else{
                $cates[$k]['img'] = null;
            }
        }
        return $cates;

    }

    // 更新分类
    public function update(Request $request){

          // 上传分类图片
          $file = $request->file("pic");
          $cateArr = json_decode($request->form,true);
  
          $cate = Cate::find($cateArr['id']);
  
          if($file){
              $path = \base_path("/public/upload/");
              $ext = $file->getClientOriginalExtension();
              $picName = time().rand(0,999).".".$ext;
              $file->move($path,$picName);
              $cate->img = $picName;
          }

        
        $cate->pid =  $cateArr['pid'];
        $cate->title = $cateArr['title'];
        $cate->des =  $cateArr['des'];
        $cate->ord = $cateArr['ord'];
        $cate->status =  $cateArr['status'];

        $bool = $cate->save();

        if($bool){
            return response()->json(['status'=>200,'msg'=>'更新成功!','data'=>$cate]);
        }else{
            return response()->json(['status'=>3000,'msg'=>'更新失败!','data'=>$cate]);
        }
        
    }

    // 更新分类状态
    public function update_state(Request $request){

        $cate = Cate::find($request->id);
        $cate->status = $request->status;
        $bool = $cate->save();

        if($bool){
            return response()->json(['status'=>200,'msg'=>'更新成功!','data'=>$cate]);
        }else{
            return response()->json(['status'=>3000,'msg'=>'更新失败!','data'=>$cate]);
        }

    }


}
